﻿@model int
@using ExpertSMS.Models;

@{
    ExpertSMSContext context = new ExpertSMSContext();
}
    @if (ViewBag.PossibleUnits == null && ViewBag.PossibleUnit == null)
    {
        using (Ajax.BeginForm("_StoreToKitchenEntryProductDetails", "StoreToKitchenEntryItems", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "productdetails", LoadingElementId = "loading" }))
        {
            <table>
                <tr>
                    <td>
                        @Html.DropDownList("productID", new SelectList((context.Products), "ProductID", "Name"))
                        @Html.Hidden("storeToKitchenEntryID", Model)
                    </td>
                    <td>
                        <input type="submit" value="เลือกสินค้า" />
                    </td>
                </tr>
            </table>
        }
    }
    else if (((Unit)ViewBag.PossibleUnit) == null)
    {
        using (Ajax.BeginForm("_StoreToKitchenEntryProductDetails", "StoreToKitchenEntryItems", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "productdetails", LoadingElementId = "loading" }))
        {
            <table>
                <tr>
                    <td>
                        @Html.Hidden("storeToKitchenEntryID", Model)
                        @Html.Encode(((Product)ViewBag.PossibleProducts).Name)
                        @Html.Hidden("productID", (((Product)ViewBag.PossibleProducts).ProductID))
                    </td>
                    <td>
                        @Html.DropDownList("unitID", new SelectList((IEnumerable<Unit>)ViewBag.PossibleUnits, "UnitID", "Name"))
                    </td>
                    <td>
                        <input type="submit" value="เลือกหน่วย" />
                    </td>
                </tr>
            </table>
        }
    }
    else
    {
        
        Unit unit = ((Unit)ViewBag.PossibleUnit);
        using (Ajax.BeginForm("Create", "StoreToKitchenEntryItems", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "productlist", LoadingElementId = "loading" }))
        {
            <table>
                <tr>
                    <td>
                        @Html.Hidden("storeToKitchenEntryID", Model)
                        @Html.Encode(((Product)ViewBag.PossibleProducts).Name)
                        @Html.Hidden("productID", (((Product)ViewBag.PossibleProducts).ProductID))
                    </td>
                    <td>
                        จำนวน : @Html.TextBox("quantity", "0.00", new { @class = "small" })
                    </td>
                    <td>
                        @Html.Encode(((Unit)ViewBag.PossibleUnit).Name)
                        [ ใน Stock=@Html.Encode((context.StoreProductStocks.Where(x=>x.ProductID == unit.ProductID).Sum(x=>x.CurrentQuantity)*((Unit)ViewBag.PossibleUnit).ConvertingFactor).ToString("n2"))  ]
                        @Html.Hidden("unitID", (((Unit)ViewBag.PossibleUnit).UnitID))
                    </td>
                    <td>
                        <input type="submit" value="เพิ่ม" />
                    </td>
                </tr>
            </table>
        }
    } 